
const app = getApp();
Page({
  data: {
    addName:'',
    pageCount: '',
    pageno: 1,
    renderObj: {
      listArr: []
    },
    tabsName: [],
    typeId: '',
    curNum: -1,
    bottomTxt: '',
    // 筛选
    filter: false,
    filterArr: '',
    ageArr: [20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45],
    sex: '',
    ageMin: '',
    ageMax: '',
    marriage: '',
    // 接口参数使用
    selectedAgeMin: '',
    selectedAgeMax: '',

    educations: '',
    income: '',
    // 地区
    cityArr: [],
    countyArr: '',
    // 挑选的市县id
    selectedCityId: '',
    selectedCountyId: '',
    region: '',

    // 打招呼
    msgPageno: 1,
    msgDialog: false,
    msgPageCount: '',
    msgList: [],
    selectMsgIndex: -1,
    selectMsg: '',
    mateId: '',
    // 发信息
   addName:wx.getStorageSync("addName"),
    mask_showFlag: true,
    hiDialog_showFlag: true,
    msgDialog_showFlag: true,
    sayHi_chooseFlag: false,//是否被选中的效果
      sayH_ilist: [],
      sayHi_pageNo: 1,
      sayHi_pageCount: '',
      sayHi_currentidx: 0,
      sayHi_msgChoosed: '',//被选中的信息
      reloadFlag:false,//刷新页面标识
  },
  //事件处理函数
  // onLoad: function (options) {
  //   this.initPage(options);
  // },
  onShow: function () {
    if(!this.data.reloadFlag){
      var _this = this;
      var userid = app.globalData.userid;
      if (!userid) {
        var openid = wx.getStorageSync('openid');
        if (!openid) {
          app.wxLogin().then(function (v) {//
            if (v) {
              app.ifLogin().then(function () {
                _this.initPage();
              })
            }
          })
        } else {
          app.getUserInfoSyn({ "openId": wx.getStorageSync('openid') }).then(function () {
            _this.initPage();
          })
        }
      } else {
        _this.initPage();
      }
    }
  },

  onHide:function(){
   this.setData({
     reloadFlag:false
   })
    wx.setStorageSync('sayHiContent', null);
    wx.setStorageSync('mateid', null);
    wx.setStorageSync('msgContent', null);
  },  
  // 筛选点击跳转
  goChooseCityPage: function () {
    wx.navigateTo({
      url: '../../pages/chooseCity/chooseCity'
    })
  },
  goQuery:function(){
     wx.navigateTo({
      url: '../../pages/memberships/memberships'
    })
    this.data.reloadFlag=false;
   },
  // 会员详情点击跳转
  Memberdetails: function (e) {
    let id = e.currentTarget.dataset.id;
    console.log(id);
    wx.navigateTo({
      url: '../../pages/memberDetails/memberDetails?userid=' + id
    });
  },
  goChoose:function(){
    wx.navigateTo({
      url: '../../pages/chooseCity/chooseCity',
    })
  },
  initPage:function(options){
    wx.showToast({
      title: '加载中',
      icon:'loading',
      mask:true,
    })
    var _this=this;
    var addName = wx.getStorageSync('addName');
    var addCode=wx.getStorageSync('addCode');
    var _params = {
      'id': app.globalData.userid,
      'pageno': _this.data.pageno,
      'pagesize': 10,
      'xgmember.marry': _this.data.marriage,
      'xgmember.money': _this.data.income,
      'xgmember.sex': _this.data.sex,
      'xgmate.ageBegin': _this.data.selectedAgeMin,
      'xgmate.ageEnd': _this.data.selectedAgeMax,
      'xgmate.code': _this.data.region,
      'xgmember.education': _this.data.educations,
      'code': addCode
    };
    var params={};
    for (var key in params) {
      _params[key] = params[key];
    }
    app.doReq(app.globalData.SERVICE.MATES.LIST, _params,'GET',function(res){
      wx.hideLoading();
      if(res.data.code==200){
        var items = res.data.pageitems;
        var data =res.data;
        if (_this.data.pageno == 1) {
          _this.setData({
            renderObj: {
              listArr:items
            },
            pageCount: data.pagecount
          });
        } else {
          _this.setData({
            renderObj: {
              listArr: _this.data.renderObj.listArr.concat(data.pageitems),
            },
            pageCount: data.pagecount
          })
        }
      }
    });
  },
  onReachBottom:function(){//刷新
  if(!this.data.reloadFlag){
    var _this = this;
    var currentPageNo = _this.data.pageno;
    if (currentPageNo <= _this.data.pageCount) {
      _this.data.pageno++;
      _this.initPage();
    }
  }
  },
  showMsgContent:function(e){
    let mateid = e.currentTarget.dataset.mateid;
    wx.setStorageSync('mateid', mateid);
    this.setData({
      mask_showFlag: false,
      hiDialog_showFlag:true,
      msgDialog_showFlag:false
    });
  },
  showHiContent:function(e){
    wx.setStorageSync('mateid',e.currentTarget.dataset.mateid)
    this.sayHiInit();
    this.setData({
      mask_showFlag: false,
      hiDialog_showFlag: false, 
      msgDialog_showFlag: true
    });
  },
  hideContent:function(e){                
    this.setData({
      mask_showFlag: true,
      hiDialog_showFlag: true,
      msgDialog_showFlag: true,
      sayH_ilist: [],
      sayHi_pageNo: 1,
      sayHi_pageCount: '',
      sayHi_currentidx: 0,
      sayHi_msgChoosed: ''//被选中的信息
    });
    wx.setStorageSync('sayHiContent', '');
  },
  whenChoose(e) {
    console.log(e);
    //记录上次点击的对象的序号
    var oldidx = this.data.sayHi_currentidx;
    //记录当前点击的对象的序号
    var currentidx = e.currentTarget.dataset.idx;
    let content = e.currentTarget.dataset.content;//选定的打招呼语句
    if (oldidx == currentidx) {
      var chooseFlag = this.data.sayHi_chooseFlag;
      this.setData({
        sayHi_currentidx: currentidx,
        sayHi_chooseFlag: !chooseFlag
      });
      wx.setStorageSync('sayHiContent', '');
    } else {
      this.setData({
        sayHi_currentidx: currentidx,
        sayHi_chooseFlag: true
      });
      wx.setStorageSync('sayHiContent', content);
    }
  },
  sayHiInit:function(){
    var _this = this;
    app.doReq(app.globalData.SERVICE.MATES.VERIFY, { id: app.globalData.userid }, 'GET', function (res) {
      if (res.data.errmsg.indexOf("择偶") > 0) {
        // 择偶条件没有完善跳转到择偶条件
        app.confirm("提示", "为更好地为您提供服务，请在“个人编辑”中完善择偶条件。").then(function (value) {
          if (value) {
            wx.navigateTo({
              url: '../../pages/personalBaseInfoEdit/personalBaseInfoEdit'
            })
          }
        });
      } else if (res.data.errmsg.indexOf("基本信息") > 0) {
        app.confirm("提示", "为更好地为您提供服务，请在“个人编辑”中完善个人资料。").then(function (value) {
          if (value) {
            wx.navigateTo({
              url: '../../pages/personalBaseInfoEdit/personalBaseInfoEdit'
            })
          }
        });
      } else if (res.data.errmsg.indexOf("形象照") > 0) {
        app.confirm("提示", "为更好地为您提供服务，请在“个人”中上传形象照、身份证、毕业证。").then(function (value) {
          if (value) {
            wx.navigateTo({
              url: '../../pages/personalBaseInfoEdit/personalBaseInfoEdit'
            })
          }
        });
      } else {
        app.doReq(app.globalData.SERVICE.MATES.GREETEDLIST, {
          'pageno': _this.data.sayHi_pageNo,
          'pagesize': 5
        }, 'GET', function (res) {
          if (res.data.code == 200 && res.data.type === 'SUCCESS') {
            let data = res.data;
            _this.setData({
              sayHi_pageCount: data.pagecount,
              sayH_ilist: data.pageitems,
            });
          }
        });
      }
    });
  },
  changeMsg() {
    var _this = this;
    if (_this.data.sayHi_pageNo >= _this.data.sayHi_pageCount) {
      _this.data.sayHi_pageNo = 1;
    } else {
      _this.data.sayHi_pageNo++;
    }
    _this.setData({
      sayH_ilist: [],
      sayHi_chooseFlag: false,//是否被选中的效果
      sayHi_currentidx: 0
    });
    wx.setStorageSync('sayHiContent', '');
    _this.sayHiInit();
  },
  doSayHi(e) {
    var _this = this;
    var content = wx.getStorageSync('sayHiContent');
    var mateid = wx.getStorageSync('mateid')
    if (mateid == app.globalData.userid) {
      wx.showToast({
        title: '不能发送给自己~',
        icon: 'none'
      });
    } else if (content == '' || content == null) {
      wx.showToast({
        title: '请选择一条打招呼内容',
        icon: 'none'
      });
    } else {
      app.doReq(app.globalData.SERVICE.MATES.SAYHI, {
        'conMsg.memberA.id': app.globalData.userid,
        'conMsg.memberB.id': mateid,
        'conMsg.msg': content,
        'conMsg.type': 1
      }, 'get', function (res) {
        let data = res.data;
        if (data.code == 200) {
          wx.showToast({
            title: '发送成功',
            icon: 'success'
          })
          wx.setStorageSync('sayHiContent', '');
          _this.hideContent();
        }
      });
    }
  },
sendMsg(){
  var _this = this;
  var mateid = wx.getStorageSync('mateid')
  var content = wx.getStorageSync('msgContent');
  app.doReq(app.globalData.SERVICE.MATES.VERIFY, { id: app.globalData.userid }, 'GET', function (res) {
    if (res.data.errmsg.indexOf("择偶") > 0) {
      // 择偶条件没有完善跳转到择偶条件
      app.confirm("提示", "为更好地为您提供服务，请在“个人编辑”中完善择偶条件。").then(function (value) {
        if (value) {
          location.href = 'uc_mate_condition.html';
        }
      });
    } else if (res.data.errmsg.indexOf("基本信息") > 0) {
      app.confirm("提示", "为更好地为您提供服务，请在“个人编辑”中完善个人资料。").then(function (value) {
        if (value) {
          location.href = 'uc_profile.html';
        }
      });
    } else if (res.data.errmsg.indexOf("形象照") > 0) {
      app.confirm("提示", "为更好地为您提供服务，为更好地为您提供服务，请在“个人”中上传形象照、身份证、毕业证。").then(function (value) {
        if (value) {
          location.href = 'uc_basicInfo.html';
        }
      });
    } else {
      app.doReq(app.globalData.SERVICE.MATES.SAYHI, {
        'conMsg.memberA.id': app.globalData.userid,
        'conMsg.memberB.id': mateid,
        'conMsg.msg': content,
        'conMsg.type': 1
      }, 'GET', function (res) {
        if (res.data.code == 200 && res.data.type === 'SUCCESS') {
          wx.showToast({
            title: '发送成功',
            icon: 'success'
          });
          _this.setData({
            showFlag: true
          });
        }
      });
    }
  });
},
  whenInput(e) {
    let content = e.detail.value;
    wx.setStorageSync('msgContent', content);
  }
})
